package com.whc.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.whc.common.to.OrderTO;
import com.whc.gulimall.order.constant.OrderMQConstant;
import com.whc.gulimall.order.entity.OrderEntity;
import com.whc.gulimall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author angaoen
 */
@Service
@Slf4j
@RabbitListener(queues = OrderMQConstant.ORDER_RELEASE_ORDER_QUEUE)
public class OrderCloseListener {

    @Resource
    private OrderService orderService;

    /**
     * 处理订单
     *
     * @param entity      订单信息
     * @param message message
     * @param channel channel
     */
    @RabbitHandler
    public void handleOrder(OrderEntity entity, Message message, Channel channel) throws IOException {
        log.info("收到处理订单信息=>{}", entity.toString());
        try {
            orderService.closeOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
